import gugga.application.ApplicationController;
import gugga.application.NavigationManager;
import gugga.application.Section;
import gugga.browser.BrowserHistory;
import gugga.browser.BrowserHistoryManager;
import gugga.commands.NavigationCommand;
import gugga.common.DeepLinksSerializer;
import gugga.navigation.MenuNavigation;

/**
 * @author stefan
 */
class historyManagement.Application 
	extends ApplicationController
{
	private var mSectionPath:String;
	
	//List all the application fields here. 
	private var mMainNavigation:MenuNavigation;
	private var mFirstSection:Section; 
	private var mSecondSection:Section; 
	private var mThirdSection:Section; 
	private var mFourthSection:Section; 
	private var mFifthSection:Section;
	
	public function Application() 
	{
		super();
		
		// The function getComponent creates a mapping between the instances on the scene and
		// the member variables of the Application class.
		mMainNavigation = getComponent("menuNavigation");
		mFirstSection = getComponent("FirstSection");
		mSecondSection = getComponent("SecondSection");
		mThirdSection = getComponent("ThirdSection");
		mFourthSection = getComponent("FourthSection");
		mFifthSection = getComponent("FifthSection");
		
		// Begin publishing navigationPath to the address bar and navigating on deep links
		
		_global["ApplicationController"] = this;
		
		BrowserHistoryManager.initialize();
	}
	
	// All Objects are instantiated inside the Application inherit UIComponentEx except macromedia components.
	// This function is required because we have to make sure that 
	// all objects inside the Application are associated with their respective classes and are avaialable
	// and ready to work.
	// All sections are phisically nested inside the main Application and their initialization is not immediate
	// before execution of that function you cannot call any event related  methods or mixin related methods of the objects.
	
	public function initUI ()
	{
		// The constructor of the super class must be called.
		super.initUI();
		
		// Configure main navigation.
		configureMenu();
		
		// Register all the sections in the application.
		registerSections();
		
		// Initialization sequence	
		// Triger initializing sequence.
		initialize();				
		
		//CommandManager.Instance.execute(new NavigationCommand("First"));
		
		NavigationManager.Instance.startNavigations();
		
		openStartupSection();
				
		// Activating sequence		
		// Using default activating sequence.
		
		// Open sequence		
		// Using default open sequence.
		
		// Close sequence		
		// Using default close sequence.		
	}
	
	private function openStartupSection() : Void
	{	
		var path : String = resolveStartupSectionPath();
		navigateTo(path);
		
		if(BrowserHistory.available)
		{
			var startupNavigationPathRecord : Object = new Object();
			DeepLinksSerializer.serializeCurrentNavigationPath(startupNavigationPathRecord);
			BrowserHistory.Instance.addHistory(startupNavigationPathRecord);
		}	
	}
	
	private function resolveStartupSectionPath() : String 
	{
		var result : String = "First";
		
		var deepLinkPath : String = DeepLinksSerializer.deserializeNavigationPath(_level0); 
		if(deepLinkPath)
		{
			result = deepLinkPath;
		}
		
		return result;	
	}
	
	private function registerSections ()
	{
		this.registerChildSection("FirstSection", "First", "First") ;
		this.registerChildSection("SecondSection", "Second", "Second");
		this.registerChildSection("ThirdSection", "Third", "Third");
		this.registerChildSection("FourthSection", "Fourth", "Fourth");
		this.registerChildSection("FifthSection", "Fifth", "Fifth");
	}
	
	private function configureMenu ()
	{
		mMainNavigation.registerChildSubItem("btn1", "First", null, new NavigationCommand("First"));
		mMainNavigation.registerChildSubItem("btn2", "Second", null, new NavigationCommand("Second"));
		mMainNavigation.registerChildSubItem("btn3", "Third", null, new NavigationCommand("Third"));
		mMainNavigation.registerChildSubItem("btn4", "Fourth", null, new NavigationCommand("Fourth"));
		mMainNavigation.registerChildSubItem("btn5", "Fifth", null, new NavigationCommand("Fifth"));
		// to do
		
		NavigationManager.Instance.registerNavigation(mMainNavigation);
	}
}